﻿using Macro_stone.IRepository.SystemManager;
using Macro_stone.IRepository.SystemManager.CommonlyUsedManager;
using Macro_stone.PoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.Repository;
using SystemCenterSoa.Repository.CommonLib;
using SystemCenterSoa.Service.SystemManager;

namespace Macro_stone.Repository.ClienteleRepository
{
    public class CustomerInputRepository: BaseRepository, ICustomerInputRepository
    {
      
        public CustomerInputRepository(SystemCenterDbSoaContext SystemCenterDbSoaContext) : base(SystemCenterDbSoaContext)
        {

        }
        /// <summary>
        /// 添加基本信息（扩展）账户密码？
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool CustomerPoAdd(CustomerPo view)
        {
            _systemCenterDbSoaContext.CustomerPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加甲方负责人
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool ResponsiblePersonPoAdd(ResponsiblePersonPo view)
        {
            _systemCenterDbSoaContext.ResponsiblePersonPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 查询甲方负责人
        /// </summary>
        /// <returns></returns>
        public List<ResponsiblePersonPo> responsiblePeopleIndex()
        {
            return _systemCenterDbSoaContext.ResponsiblePersonPo.AsQueryable().ToList();
        }


        /// <summary>
        /// 添加满意度调查
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool SatisfactionSurveyPoAdd(SatisfactionSurveyPo view)
        {
            _systemCenterDbSoaContext.SatisfactionSurveyPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool AccessoryPoAdd(AccessoryPo view)
        {
            _systemCenterDbSoaContext.AccessoryPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 客户信息—显示(基础列表)
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public  List<CustomerPo> CustomerPoInquire(int pageIndex,int pageSize, int? createTime,string customerName, DateTime? beginTime,DateTime? endTime, string customerSerialNum, string linkman)
        {
            var query = _systemCenterDbSoaContext.CustomerPo.AsQueryable();
                if(createTime!=null && createTime>0)
                {
                    //年份，日期
                    query = query.Where(p => p.createTime.Year == createTime);
                }
                if (!string.IsNullOrWhiteSpace(customerName))
                {
                    //客户名称
                    query = query.Where(p => p.customerName.Contains(customerName));
                }
                if (beginTime!=null )
                {
                    //时间，低于某时间，日期
                    query = query.Where(p => p.createTime< beginTime);
                }
                if (endTime!=null)
                {
                    //时间，高于某时间，日期
                    query = query.Where(p => p.createTime > endTime);
                }
                if ( !string.IsNullOrEmpty(customerSerialNum))
                {
                    //客户编号
                    query = query.Where(p => p.dustomerSerialNum.Contains(customerSerialNum));
                }
                if (!string.IsNullOrEmpty(linkman))
                {
                    //联系人
                    query = query.Where(p => p.linkman.Contains(linkman));
                }

                int totalCount = query.Count();

            int ssa = (pageIndex - 1) * pageSize;
            List<CustomerPo> coude = query.OrderBy(p => p.customerId).Skip(ssa).Take(pageSize).ToList();

            
            //throw new NotImplementedException();
            return coude;
        }

        /// <summary>
        /// 联系人信息—显示
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public List<CustomerPo> ResponsiblePersonPoInquire(int pageIndex, int pageSize, string personName,DateTime? entryTime,DateTime? abortTime)
        {
            var query = _systemCenterDbSoaContext.CustomerPo.AsQueryable();
                if ( !string.IsNullOrEmpty(personName))
                {
                    //客户名称
                    query = query.Where(p => p.customerName.Contains(personName));
                }
                if (entryTime!=null)
                {
                    //时间，高于某时间，日期
                    query = query.Where(p => p.createTime > entryTime);
                }
                if ( abortTime!=null)
                {
                    //时间，低于某时间，日期
                    query = query.Where(p => p.createTime < abortTime);
                }
            int ssa = (pageIndex - 1) * pageSize;
            List<CustomerPo> coude = query.OrderBy(p => p.customerId).Skip(ssa).Take(pageSize).ToList();


            //throw new NotImplementedException();
            return coude;
        }

        /// <summary>
        /// 联系人信息—反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ResponsiblePersonPoBackfill(int id)
        {
            _systemCenterDbSoaContext.ResponsiblePersonPo.FirstOrDefault(p => p.serialId == id);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 项目前期信息—显示
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public List<AccessoryPo> AccessoryDtoInquire(int pageIndex,int pageSize, string wenName,DateTime? begintime,DateTime? endtime)
        {
            var query = _systemCenterDbSoaContext.AccessoryPo.AsQueryable();
            if (!string.IsNullOrEmpty(wenName))
            {
                //客户名称
                query = query.Where(p => p.fileName.Contains(wenName));
            }
            if (begintime != null)
            {
                //时间，高于某时间，日期see
                query = query.Where(p => p.uploadTime > begintime);
            }
            if (endtime != null)
            {
                //时间，低于某时间，日期
                query = query.Where(p => p.uploadTime < endtime);
            }
            int ssa = (pageIndex - 1) * pageSize;
            List<AccessoryPo> coude = query.OrderBy(p => p.fileId).Skip(ssa).Take(pageSize).ToList();
            //throw new NotImplementedException();
            return coude;
        }

        /// <summary>
        /// 项目前期信息—删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool AccessoryDtoDelete(int ids)
        {
            var ssa = _systemCenterDbSoaContext.AccessoryPo.FirstOrDefault(p => p.fileId == ids);
            _systemCenterDbSoaContext.AccessoryPo.Remove(ssa);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 项目前期信息—反填一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AccessoryPo AccessoryPoOne(int id)
        {
            return _systemCenterDbSoaContext.AccessoryPo.FirstOrDefault(p => p.fileId == id);
            
        }

        /// <summary>
        /// 项目前期信息—编辑
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool AccessoryDtoEdits(AccessoryPo view)
        {
            _systemCenterDbSoaContext.Entry(view).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        } 
        /// <summary>
        /// 项目前期信息—添加
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool AccessoryDtoAdd(AccessoryPo view)
        {
            _systemCenterDbSoaContext.AccessoryPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }




    }
}
